TARGET	= menulib
OBJS	= ./build/main.o ./build/include/generic/callbacks.o ./build/include/menu/menu.o ./build/include/functions/thread_functions.o ./build/include/ctrlhandler/ctrlHandler.o
ASMOBJS =

# Spécification des dossiers de build et de stockage des binaires pour chaque type de cible

BUILD_DIR = ./build
BIN_DIR = ./bin
SRC_DIR = ./src

# Spécification des flags pour le compilateur

INCDIR	=
CFLAGS	= -G0 -Wall -O2
CXXFLAGS= $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS	= $(CFLAGS)

# Spécification des chemins pour les librairies

LIBDIR	=
LDFLAGS	=
LIBS	=

BUILD_PRX =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Menulib
#PSP_EBOOT_ICON =
#PSP_EBOOT_PIC1 =
#PSP_EBOOT_SND0 =

PSPSDK=/usr/local/pspsdk/psp/sdk
PSPLIBSDIR = $(PSPSDK)/..

CC	= psp-gcc
CXX	= psp-g++
AS	= psp-gcc
ASM	= psp-as
LD	= psp-ld
AR	= psp-ar
RANLIB	= psp-ranlib
STRIP	= psp-strip
MKSFO    = mksfo
PACK_PBP = pack-pbp
FIXUP    = psp-fixup-imports

# Add in PSPSDK includes and libraries.
INCDIR   := $(INCDIR) . $(PSPSDK)/include
LIBDIR   := $(LIBDIR) . $(PSPSDK)/lib

ASMFLAGS = -D PSP -I $(INCDIR) -W -Wall -O2 -G0 -fno-pic -mno-abicalls -w -fomit-frame-pointer

CFLAGS   := $(addprefix -I,$(INCDIR)) $(CFLAGS)

PSP_FW_VERSION=150
CFLAGS += -D_PSP_FW_VERSION=$(PSP_FW_VERSION)
CXXFLAGS += -D_PSP_FW_VERSION=$(PSP_FW_VERSION)

LDFLAGS  := $(addprefix -L,$(LIBDIR)) $(LDFLAGS)

#Use newlib as libc
PSPSDK_LIBC_LIB = -lpsplibc
CFLAGS := -I$(PSPSDK)/include/libc $(CFLAGS)
CXXFLAGS := -I$(PSPSDK)/include/libc $(CXXFLAGS)

#Link with newlib libraries
PSPSDK_LIBS = -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk
LIBS     := $(LIBS) $(PSPSDK_LIBS) $(PSPSDK_LIBC_LIB) -lpspnet \
			-lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility \
			-lpspuser -lpspkernel

# Define the overridable parameters for EBOOT.PBP
ifndef PSP_EBOOT_TITLE
PSP_EBOOT_TITLE = $(TARGET)
endif

ifndef PSP_EBOOT_SFO
PSP_EBOOT_SFO = PARAM.SFO
endif

ifndef PSP_EBOOT_ICON
PSP_EBOOT_ICON = NULL
endif

ifndef PSP_EBOOT_ICON1
PSP_EBOOT_ICON1 = NULL
endif

ifndef PSP_EBOOT_UNKPNG
PSP_EBOOT_UNKPNG = NULL
endif

ifndef PSP_EBOOT_PIC1
PSP_EBOOT_PIC1 = NULL
endif

ifndef PSP_EBOOT_SND0
PSP_EBOOT_SND0 = NULL
endif

ifndef PSP_EBOOT_PSAR
PSP_EBOOT_PSAR = NULL
endif

ifndef PSP_EBOOT
PSP_EBOOT = EBOOT.PBP
endif

rebuild: clean all

# clean
clean:
	-rm -f $(FINAL_TARGET) $(EXTRA_CLEAN) $(OBJS) $(BUILD_DIR)/$(PSP_EBOOT_SFO) $(BIN_DIR)/$(PSP_EBOOT) $(BIN_DIR)/$(EXTRA_TARGETS)

# all
all: $(EXTRA_TARGETS) $(FINAL_TARGET)

#Insert here all the files to compile
compile_deps :
	$(CXX) $(CXXFLAGS) -c -o $(BUILD_DIR)/include/generic/callbacks.o $(SRC_DIR)/include/generic/callbacks.cpp
	$(CXX) $(CXXFLAGS) -c -o $(BUILD_DIR)/include/functions/thread_functions.o $(SRC_DIR)/include/functions/thread_functions.cpp
	$(CXX) $(CXXFLAGS) -c -o $(BUILD_DIR)/include/ctrlhandler/ctrlHandler.o $(SRC_DIR)/include/ctrlhandler/ctrlHandler.cpp
	$(CXX) $(CXXFLAGS) -c -o $(BUILD_DIR)/include/menu/menu.o $(SRC_DIR)/include/menu/menu.cpp
	$(CXX) $(CXXFLAGS) -c -o $(BUILD_DIR)/main.o $(SRC_DIR)/main.cpp

#Do not forget to compile ASM dependencies before compiling program
compile_deps_asm :
	$(ASM) -o $(BUILD_DIR)/asm/fichier $(SRC_DIR)/asm/fichier

#Macro which compiles the .elf file
$(TARGET).elf: $(OBJS) $(EXPORT_OBJ)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(LIBS) -o $(BUILD_DIR)/$@
	$(FIXUP) $(BUILD_DIR)/$@

$(TARGET)-asm.elf: $(ASMOBJS)
	$(LD) -T linker.x -L$(PSPSDK)/lib main.o -o $@

$(PSP_EBOOT)-asm:
	$(TARGET)-asm.elf $(PSP_EBOOT_SFO) compile_deps_asm
	$(STRIP) $(BUILD_DIR)/$(TARGET)-asm.elf -o $(BUILD_DIR)/$(TARGET)-asm_strip.elf
	$(PACK_PBP) $(BIN_DIR)/$(PSP_EBOOT) $(BUILD_DIR)/$(PSP_EBOOT_SFO) $(PSP_EBOOT_ICON)  \
		$(PSP_EBOOT_ICON1) $(PSP_EBOOT_UNKPNG) $(PSP_EBOOT_PIC1)  \
		$(PSP_EBOOT_SND0)  $(BUILD_DIR)/$(TARGET)-asm_strip.elf $(PSP_EBOOT_PSAR)
	-rm -f $(BUILD_DIR)/$(TARGET)_strip.elf


#Macro which generates the appropriate SFO in order to name the program in the eboot
$(PSP_EBOOT_SFO): 
	$(MKSFO) '$(PSP_EBOOT_TITLE)' $(BUILD_DIR)/$@

#High-level macro which generates the EBOOT.PBP output file 
$(PSP_EBOOT): compile_deps $(TARGET).elf $(PSP_EBOOT_SFO)
	$(STRIP) $(BUILD_DIR)/$(TARGET).elf -o $(BUILD_DIR)/$(TARGET)_strip.elf
	$(PACK_PBP) $(BIN_DIR)/$(PSP_EBOOT) $(BUILD_DIR)/$(PSP_EBOOT_SFO) $(PSP_EBOOT_ICON)  \
		$(PSP_EBOOT_ICON1) $(PSP_EBOOT_UNKPNG) $(PSP_EBOOT_PIC1)  \
		$(PSP_EBOOT_SND0)  $(BUILD_DIR)/$(TARGET)_strip.elf $(PSP_EBOOT_PSAR)
	-rm -f $(BUILD_DIR)/$(TARGET)_strip.elf

%.c: %.exp
	psp-build-exports -b $< > $@


